package
{
	import de.derhess.video.vimeo.*;
	
	import flash.display.Sprite;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * This class is the remote controll for the AirVimeoPlayer component. So
	 * this class directly controlls the vimeo Player moogaloop API
	 * 
	 * @author Florian Weil
	 * @see http://blog.derhess.de
	 */
	
	public class RemoteVimeoController extends Sprite
	{
		private var vimeo_player:VimeoPlayer;
		private var receiverLC:LocalConnection;
		private var airRemoteLC:LocalConnection;
		private var command:VimeoAirCommand;
		private var videoClipID:int = 0;
		private var lastSentTime:Number = 0;
		private var playerState:String = "";
		
		public function RemoteVimeoController()
		{
			init();
		}
		
		
		private function init():void
		{
			
			command = new VimeoAirCommand();
			
			// Create LocalConnection for the vimeo Remote - to receive commands from air component
			receiverLC = new LocalConnection();
			receiverLC.allowDomain("*");
			receiverLC.client = this;
            //try {
                receiverLC.connect(VimeoAirAttributes.VIMEO_REMOTE);
            /*} catch (error:ArgumentError) {
                trace("Can't connect...the connection name is already being used by another SWF");
            }*/
            
            
            // Create LocalConnection for the air Remote - to send commands to the air component
            airRemoteLC = new LocalConnection();
			airRemoteLC.client = this;
			airRemoteLC.allowDomain("*");
			airRemoteLC.allowInsecureDomain();
			airRemoteLC.addEventListener(StatusEvent.STATUS, handleLCStatus);
			// TODO Implement Handler for Async_Error
			//vimeo_lc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			// TODO Implement Handler for Security_Error
			//vimeo_lc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		}
		
	
		//--------------------------------------------------------------------------
        //
        //  Methods
        //
        //--------------------------------------------------------------------------
		private function sendCommand(cmd:VimeoAirCommand):void 
		{
			airRemoteLC.send(VimeoAirAttributes.AIR_REMOTE, VimeoAirAttributes.AIR_REMOTE_FUNC, cmd);
        }
		
		
		public function remote_vimeo_handler(cmd:*):void
		{
			switch (cmd.command)
			{
				case VimeoAirAttributes.COMMAND_PLAY:
				{
					if(vimeo_player)
						vimeo_player.play();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_PAUSE:
				{
					if(vimeo_player)
						vimeo_player.pause();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_STOP:
				{
					if(vimeo_player)
						vimeo_player.stop();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SEEK:
				{
					if(vimeo_player)
						vimeo_player.seekTo(int(cmd.arguments));
					break;
				}
				
				case VimeoAirAttributes.COMMAND_START_VIMEO_PLAYER:
				{
					videoClipID = int(cmd.arguments.id);
					var player_width:int = int(cmd.arguments.width);
					var player_height:int = int(cmd.arguments.height);
					
					vimeo_player = new VimeoPlayer(videoClipID, player_width, player_height);
					vimeo_player.x = vimeo_player.y = 0;
					vimeo_player.visible = false;
					//VimeoPlayer.MAX_REPEAT_SAME_CURRENT_TIME = 15;
					vimeo_player.addEventListener(VimeoEvent.PLAYER_LOADED, handlePlayerLoaded);	
					vimeo_player.addEventListener(VimeoEvent.DURATION, handleDuration);
					vimeo_player.addEventListener(VimeoEvent.STATUS, handleVimeoStatus);
					addChild(vimeo_player);
					break;
				}
				
				case VimeoAirAttributes.COMMAND_PLAY_NEW_VIDEO:
				{
					// This is important for the use of the VimeoWrapper Class
					if(vimeo_player)
						vimeo_player.loadVideo(int(cmd.arguments));
					break;
				}
				
				case VimeoAirAttributes.COMMAND_PLAYER_COLOR:
				{
					if(vimeo_player)
						vimeo_player.changeColor(String(cmd.arguments));
					break;
				}
				
				case VimeoAirAttributes.COMMAND_VOLUME:
				{
					if(vimeo_player)
						vimeo_player.setVolume(Number(cmd.arguments));
					break;
				}
				
				case VimeoAirAttributes.COMMAND_VIDEO_SIZE:
				{
					if(vimeo_player)
						vimeo_player.setSize(int(cmd.arguments.width),int(cmd.arguments.height));
					break;
				}
				
				case VimeoAirAttributes.COMMAND_UNLOAD_VIDEO:
				{
					if(vimeo_player)
						vimeo_player.unloadVideo();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_DESTROY:
				{
					if(vimeo_player)
						vimeo_player.destroy();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_TOGGLE_LOOP:
				{
					if(vimeo_player)
						vimeo_player.toggleLoop();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_HD_ON:
				{
					if(vimeo_player)
						vimeo_player.hd_on();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_HD_OFF:
				{
					if(vimeo_player)
						vimeo_player.hd_off();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SHOW_LIKE:
				{
					if(vimeo_player)
						vimeo_player.showLikeScreen();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SHOW_CONTROLS:
				{
					if(vimeo_player)
						vimeo_player.showVimeoScreenControlls();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SHOW_EMBED:
				{
					if(vimeo_player)
						vimeo_player.showEmbedScreen();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SHOW_HD:
				{
					if(vimeo_player)
						vimeo_player.showHDScreen();
					break;
				}
				
				case VimeoAirAttributes.COMMAND_SHOW_SHARE:
				{
					if(vimeo_player)
						vimeo_player.showShareScreen();
					break;
				}
				
				default:
				{
					vimeo_player.alpha = 0.5; 
					break;
				}
			}
		}
		
		//--------------------------------------------------------------------------
        //
        //  EventHandler
        //
        //--------------------------------------------------------------------------
		///////////////////////////////////////////////////////////////////
		/////   Getting Data from Vimeo - Eventhandling Vimeo Player
		///////////////////////////////////////////////////////////////////
		private function handlePlayerLoaded(e:VimeoEvent):void
		{
			
			vimeo_player.loadVideo(videoClipID);
			vimeo_player.visible = true;
			
			command.command = VimeoAirAttributes.CALL_PLAYER_LOADED;
			command.arguments = e;
			sendCommand(command);
			
		}
		
		private function handleDuration(e:VimeoEvent):void
		{
			command.command = VimeoAirAttributes.CALL_DURATION;
			command.arguments = e;
			sendCommand(command);
		}
		
		private function handleVimeoStatus(e:VimeoEvent):void
		{
			command.command = VimeoAirAttributes.CALL_VIDEO_STATE;
			command.arguments = e;
			
			// Because of perfomance problems, we should send less messages over localConnection
			if(e.info == VimeoPlayingState.PLAYING)
			{
				//var diff:Number = Math.abs(e.currentTime - lastSentTime);
				//if(diff > 0.7)
				//{
					sendCommand(command);
					lastSentTime = e.currentTime;
				//}
				playerState = e.info;
			}
				else if((e.info == VimeoPlayingState.BUFFERING) && (playerState == VimeoPlayingState.BUFFERING))
			{
				// do nothing, because, we have already sent this PlayingState Message
			}
				else
			{
				playerState = e.info;
				sendCommand(command);
			}
		}
		
		///////////////////////////////////////////////////////////////////
		/////  Handle Local Connection Events
		///////////////////////////////////////////////////////////////////
		private function handleLCStatus(event:StatusEvent):void
		{
            switch (event.level) {
                case "status":
                {
                   //trace("ok");
                }
                case "error":
                {
                 	//trace("Failure");   
                }
            }
		}
	} // End of class
}




